import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'cart_page.dart';
import 'category_page.dart';
import 'home_page.dart';
import 'member_page.dart';
import '../provider/current_index.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.home), title: Text('首页'),),
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.search), title: Text('分类'),),
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.shopping_cart), title: Text('购物车'),),
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.profile_circled), title: Text('会员中心'),)
  ];

  final List<Widget> tabBodies = [
    HomePage(),
    CategoryPage(),
    CartPage(),
    MemberPage()
  ];

  @override
  Widget build(BuildContext context) {
    // 初始化屏幕适配
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return Consumer(
      builder: (context, CurrentIndexProvider currentIndexProvider, child) {
        return Scaffold(
          backgroundColor: Color.fromRGBO(244, 244, 244, 1.0),
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed, 
            currentIndex: currentIndexProvider.currentIndex,
            items: bottomTabs,
            onTap: (index){
              currentIndexProvider.changIndex(index);
            },
          ),
          body: IndexedStack(
            index: currentIndexProvider.currentIndex,
            children: tabBodies,
          )
        );
      },
    );
  }
}